package lt.inventi.wsdlmerger;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.muse.tools.generator.WsdlMerge;
import org.w3c.dom.Document;

/**
 * 
 * @goal merge
 * @requiresProject true
 */
public class MavenPlugin extends AbstractMojo {
	
	/**
	 * @parameter
	 */
	private String uri;

	/**
	 * @parameter
	 */
	private URL url;
	
	/**
	 * @parameter
	 */
	private File[] wsdls;
	
	/**
	 * @parameter
	 */
	private File result;
	
	/**
	 * @parameter
	 */
	private File rmd;
	
	/**
	 * @parameter
	 */
	private Boolean overwrite;
	
	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("uri:" + uri);
		getLog().info("url:" + url);
		if (wsdls != null){
			for (File wsdl: wsdls){
				getLog().info("wsdl:" + wsdl);
			}
		}
		getLog().info("result:" + result);
		getLog().info("rmd:" + rmd);
		String rmd = this.rmd == null ? null : this.rmd.toString();
		boolean overwrite = this.overwrite == null ? false : this.overwrite;
		getLog().info("overwrite:" + overwrite);
		try {
			new WsdlMerge(new Logger(){
				public void logInfo(final String msg) {
					getLog().info(msg);
				}})
			.run(wsdls, result.toString(), rmd, overwrite, 
					uri, url.toString());
		}catch (Exception e){
			throw new MojoExecutionException("Error while merging wsdls", e);
		}
	}
	
	public static interface Logger{
		public void logInfo(String msg);
	}

}
